<?php

namespace app\modules\clinical\models;

use Yii;

/**
 * This is the model class for table "excel_pdx".
 *
 * @property integer $pk_id
 * @property string $pt_birth
 * @property string $pt_time
 * @property integer $pt_hoehn_yahr
 * @property double $pt_weight
 * @property double $pt_height
 * @property double $pt_bmi
 * @property double $pt_gongsan
 * @property integer $ty_test
 * @property integer $draw_timer
 * @property integer $ntr_1
 * @property integer $ntr_2
 * @property integer $ntr_3
 * @property integer $ntr_4
 * @property integer $ntr_5
 * @property integer $ntr_6
 * @property integer $ntr_7
 * @property integer $ntr_8
 * @property integer $ntr_9
 * @property integer $ntr_10
 * @property integer $ntr_11
 * @property integer $ntr_12
 * @property integer $ntr_13
 * @property integer $ntr_14
 * @property integer $ntr_15
 * @property integer $ntr_16
 * @property integer $ntr_17
 * @property integer $ntr_18
 */
class ExcelPdx extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'excel_pdx';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['pt_birth', 'pt_time'], 'safe'],
            [['pt_hoehn_yahr', 'ty_test', 'draw_timer', 'ntr_1', 'ntr_2', 'ntr_3', 'ntr_4', 'ntr_5', 'ntr_6', 'ntr_7', 'ntr_8', 'ntr_9', 'ntr_10', 'ntr_12', 'ntr_14', 'ntr_15', 'ntr_18'], 'integer'],
            [['pt_weight', 'pt_height', 'pt_bmi', 'pt_gongsan', 'ntr_11', 'ntr_13', 'ntr_16', 'ntr_17'], 'number']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'pt_birth' => '出生年月',
            'pt_time' => '发病时间',
            'pt_hoehn_yahr' => 'Hoehn-Yahr分级',
            'pt_weight' => '体重（kg）',
            'pt_height' => '身高（cm）',
            'pt_bmi' => 'BMI',
            'pt_gongsan' => '肱三头肌皮褶厚度（mm）',
            'ty_test' => '洼田饮水试验',
            'draw_timer' => '画钟试验总分',
        ];
    }

    public static function getParams($type = false, $code = false)
    {
        $params = [
            'ty_test' => [1 => '1级', 2 => '2级', 3 => '3级', 4 => '4级', 5 => '5级'],
        ];

        return ($type) ? $params[$type] : [];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {

            $this->pt_birth = $this->pt_birth ? strtotime($this->pt_birth) : NULL;
            $this->pt_time = $this->pt_time ? strtotime($this->pt_time) : NULL;
            return true;

        } else{
            return false;
        }
    }

    public function load($data, $formName = null)
    {
        parent::load($data, $formName);

        $this->pt_time = $this->__clear_date($this->pt_time);

        if (isset($data['ExcelPdx']['ntr_11'])) {

            $this->ntr_11 = $this->ntr_11 ? floatval($data['ExcelPdx']['ntr_11']) : NULL;
            $this->ntr_13 = $this->ntr_13 ? floatval($data['ExcelPdx']['ntr_13']) : NULL;
            $this->ntr_16 = $this->ntr_16 ? floatval($data['ExcelPdx']['ntr_16']) : NULL;
            $this->ntr_17 = $this->ntr_17 ? floatval($data['ExcelPdx']['ntr_17']) : NULL;
        }

        if ($this->getTotal('part1') >= 12) {
            $this->ntr_7 = NULL;
            $this->ntr_8 = NULL;
            $this->ntr_9 = NULL;
            $this->ntr_10 = NULL;
            $this->ntr_11 = NULL;
            $this->ntr_12 = NULL;
            $this->ntr_13 = NULL;
            $this->ntr_14 = NULL;
            $this->ntr_15 = NULL;
            $this->ntr_16 = NULL;
            $this->ntr_17 = NULL;
            $this->ntr_18 = NULL;
        }

        if (isset($data['ExcelPdx'])) {
            return true;
        }

        return false;
    }

    public function defaultValue()
    {
        $pt_birth = Patient::findOne(Yii::$app->session->get('pt_id'))->pt_birth;
        if (!empty($pt_birth)) {
            $this->pt_birth = strtotime($pt_birth);
        }
        $this->pt_time = NULL;
    }

    public function clear()
    {
        $this->ntr_11 = $this->ntr_11 ? str_pad(strval($this->ntr_11), 3, ".0") : NULL;
        $this->ntr_13 = $this->ntr_13 ? str_pad(strval($this->ntr_13), 3, ".0") : NULL;
        $this->ntr_16 = $this->ntr_16 ? str_pad(strval($this->ntr_16), 3, ".0") : NULL;
        $this->ntr_17 = $this->ntr_17 ? str_pad(strval($this->ntr_17), 3, ".0") : NULL;
    }

    public function getTotal($part = NULL)
    {
        $ntr_1 = !empty($this->ntr_1) ? $this->ntr_1 : 0;
        $ntr_2 = !empty($this->ntr_2) ? $this->ntr_2 : 0;
        $ntr_3 = !empty($this->ntr_3) ? $this->ntr_3 : 0;
        $ntr_4 = !empty($this->ntr_4) ? $this->ntr_4 : 0;
        $ntr_5 = !empty($this->ntr_5) ? $this->ntr_5 : 0;
        $ntr_6 = !empty($this->ntr_6) ? $this->ntr_6 : 0;
        $ntr_7 = !empty($this->ntr_7) ? $this->ntr_7 : 0;
        $ntr_8 = !empty($this->ntr_8) ? $this->ntr_8 : 0;
        $ntr_9 = !empty($this->ntr_9) ? $this->ntr_9 : 0;
        $ntr_10 = !empty($this->ntr_10) ? $this->ntr_10 : 0;
        $ntr_11 = !empty($this->ntr_11) ? $this->ntr_11 : 0;
        $ntr_12 = !empty($this->ntr_12) ? $this->ntr_12 : 0;
        $ntr_13 = !empty($this->ntr_13) ? $this->ntr_13 : 0;
        $ntr_14 = !empty($this->ntr_14) ? $this->ntr_14 : 0;
        $ntr_15 = !empty($this->ntr_15) ? $this->ntr_15 : 0;
        $ntr_16 = !empty($this->ntr_16) ? $this->ntr_16 : 0;
        $ntr_17 = !empty($this->ntr_17) ? $this->ntr_17 : 0;
        $ntr_18 = !empty($this->ntr_18) ? $this->ntr_18 : 0;

        $part1 = $ntr_1 + $ntr_2 + $ntr_3 + $ntr_4 + $ntr_5 + $ntr_6;

        $part2 = $ntr_7 + $ntr_8 + $ntr_9 + $ntr_10 + $ntr_11 + $ntr_12 + $ntr_13 + $ntr_14 + $ntr_15 + $ntr_16 + $ntr_17 + $ntr_18;

        if ($part == 'part1') return $part1;
        if ($part == 'part2') return $part2;

        return $part1 + $part2;
    }

    protected function __clear_date($date)
    {
        return preg_replace('/[_|-]*/', '', $date);
    }
}
